package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.unionwar.UnionWarDispatchDetail;
import com.yanqu.road.entity.activity.unionwar.result.CurrentTimeInfo;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;

@Cmd(code = Protocol.S_UNION_WAR_UNION_BATTLE_REPORT_LIST,desc = "商会战况（从排行榜入口）")
public class UnionWarBattleReportListCmd extends AbstractCommand {

    private int activityId;
    private String unionUid;
    private long unionServerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.UnionBattleReportListReqMsg reqMsg = UnionWarActivityServerProto.UnionBattleReportListReqMsg.parseFrom(packet.getBytes());
        activityId  = reqMsg.getActivityId();
        unionUid = reqMsg.getUnionUid();
        unionServerId = reqMsg.getUnionServerId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //跨服查询
        if(unionServerId != GameServer.getInstance().getServerId()){
            UnionWarActivityServerProto.UnionBattleReportListReqMsg.Builder crossReqMsg =  UnionWarActivityServerProto.UnionBattleReportListReqMsg.newBuilder();
            crossReqMsg.setUnionUid(unionUid);
            crossReqMsg.setActivityId(activityId);
            crossReqMsg.setUnionServerId(unionServerId);
            //player.sendChannelPacket(Protocol.C_CROSS_UNION_WAR_UNION_BATTLE_REPORT_LIST, crossReqMsg);
            player.sendUnionActivityGroupPacket(Protocol.C_CROSS_UNION_WAR_UNION_BATTLE_REPORT_LIST, crossReqMsg, activityId);
            return;
        }

        UnionWar unionWar = UnionPhaseWarMgr.getUnionWarMap().get(activityId);
        if(unionWar != null && !StringUtils.isNullOrEmpty(unionUid)) {
            CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
            if (currentTimeInfo.getTimeInfo() != null || currentTimeInfo.getPeriodType() != null) {
                UnionWarActivityServerProto.UnionBattleReportListRespMsg.Builder respMsg = UnionWarActivityServerProto.UnionBattleReportListRespMsg.newBuilder();
                respMsg.setActivityId(activityId);
                respMsg.setUnionUid(unionUid);
                respMsg.setRet(0);
                Map<Integer, UnionWarDispatchDetail> detailMap = unionWar.getUnionWarDispatchDetailList(unionUid, 1);
                build(respMsg, detailMap);
                detailMap = unionWar.getUnionWarDispatchDetailList(unionUid, 2);
                build(respMsg, detailMap);

                CrossUnionInfo crossUnionInfo = CrossUnionMgr.getUnionInfo(unionUid);
                if (null != crossUnionInfo){
                    int crossServerCount = CrossUnionServerGroupMgr.getGroupServerCountByServerId(crossUnionInfo.getServerId());
                    int maxMemberCount = UnionConfigMgr.getMaxMemberCount(crossUnionInfo.getExperience(), crossUnionInfo.isCross(), crossServerCount);
                    respMsg.setMaxMemberCount(maxMemberCount);
                }

                player.sendPacket(Protocol.U_UNION_WAR_UNION_BATTLE_INFO, respMsg);
            }
        }
    }

    private void build(UnionWarActivityServerProto.UnionBattleReportListRespMsg.Builder respMsg, Map<Integer, UnionWarDispatchDetail> detailMap) {
        if (detailMap != null) {
            for (UnionWarDispatchDetail detail : detailMap.values()) {
                if(!respMsg.hasUnionInfo()){
                    UnionBaseInfo unionBaseInfo = CrossUnionMgr.parseUnionBaseInfo(detail.getUnionUid());
                    if (unionBaseInfo != null) {
                        respMsg.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(unionBaseInfo));
                    }
                }
                UnionWarActivityServerProto.UnionBattleReportTemp.Builder builder = UnionWarActivityServerProto.UnionBattleReportTemp.newBuilder();
                builder.setPhase(detail.getPhase());
                builder.setSession(detail.getSession());
                builder.setWinMark(detail.getWinMark());
                UnionProto.UnionBaseTempMsg.Builder unionBaseTempMsg = UnionBasePb.parseUnionBaseTempMsg(detail.getEnemyUnionInfo());
                if (unionBaseTempMsg != null) {
                    builder.setEnemyUnionInfo(unionBaseTempMsg);
                }
                respMsg.addReport(builder);
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
